//
//  WYCPreviewController.m
//  快付钱包
//
//  Created by 中企互联 on 2018/1/19.
//  Copyright © 2018年 中企互联. All rights reserved.
//

#import "WYCPreviewController.h"
#import "WYCpreModel.h"
#import "WYCAmoutMdel.h"
#import "WYCPreCell.h"
#import "WYCSeeBillController.h"

@interface WYCPreviewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)CALayer *bottomLineLayer;
@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong) NSString *total;
@property(nonatomic,strong)UILabel *Statement1;
@property(nonatomic,strong) UILabel *Repaymentlabel1;
@property(nonatomic,strong) NSString *planNo;

@property(nonatomic,strong)NSMutableArray *  dataArray;
@property(nonatomic,strong)NSMutableArray *  detailsArray;

/** 遮罩*/
@property(nonatomic,strong)UIView * ejectView;

/** 显示View*/
@property(nonatomic,strong)UIView * showView;

@end

@implementation WYCPreviewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [[NSMutableArray alloc] init];
    
    self.detailsArray = [[NSMutableArray alloc] init];
    [self setupNav];
    //tableView头部
    [self setupTableView];
    //网络请求
    [self loadplan];
    
    [self setupbottomView];
}
- (void)setupTableView{
    
    UIView  *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 80)];
    headview.backgroundColor = [UIColor whiteColor];
    headview.userInteractionEnabled = YES;
    UILabel *Statement = [[UILabel alloc] init];
    Statement.frame = CGRectMake(10,20, 100, 30);
    Statement.text = @"还款总金额:";
    Statement.textAlignment = NSTextAlignmentLeft;
    Statement.textColor = [UIColor blackColor];
    Statement.font = [UIFont systemFontOfSize:15];
    [headview addSubview:Statement];
    
    //还款日
    UILabel *Repaymentlabel = [[UILabel alloc] init];
    Repaymentlabel.frame = CGRectMake(10, 50, 100, 30);
    Repaymentlabel.text = @"总计手续费:";
    Repaymentlabel.textAlignment = NSTextAlignmentLeft;
    Repaymentlabel.textColor = [UIColor blackColor];
    Repaymentlabel.font = [UIFont systemFontOfSize:15];
    [headview addSubview:Repaymentlabel];
    
    //总金额
    _Statement1= [[UILabel alloc] init];
    _Statement1.frame = CGRectMake(ScreenWidth-120,20, 100, 30);
    _Statement1.textAlignment = NSTextAlignmentRight;
    _Statement1.textColor = WYCMain;
    //    _Statement1.text = @"9000";
    _Statement1.font = [UIFont systemFontOfSize:15];
    
    [headview addSubview:_Statement1];
    
    //手续费
    _Repaymentlabel1 = [[UILabel alloc] init];
    _Repaymentlabel1.frame = CGRectMake(ScreenWidth-120, 50, 100, 30);
    _Repaymentlabel1.textAlignment = NSTextAlignmentRight;
    _Repaymentlabel1.textColor = WYCMain;
    _Repaymentlabel1.font = [UIFont systemFontOfSize:15];
    //    _Repaymentlabel1.text = @"6.3";
    [headview addSubview:_Repaymentlabel1];
    if (kDevice_Is_iPhoneX) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth,ScreenHeight - 88 - 20) style:UITableViewStyleGrouped];
        self.tableView.backgroundColor = WYCRGBColor(249, 249, 249);
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.tableHeaderView = headview;
        [self.view addSubview:self.tableView];
    }else{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth,ScreenHeight - 64 - 20) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = WYCRGBColor(249, 249, 249);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableHeaderView = headview;
    [self.view addSubview:self.tableView];
    }
    
}
- (void)alreadylabel{
    
    [self.navigationController popViewControllerAnimated:NO];
}
//底部还款
- (void)setupbottomView{
    //底部view
    UIView * bottomView = [[UIView alloc] init];
    bottomView.frame = CGRectMake(0, ScreenHeight - 160 * px, ScreenWidth, 160 * px);
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    //重新创建计划
    UIButton * alreadylabel = [[UIButton alloc] init];
    alreadylabel.frame = CGRectMake(0, 0, bottomView.width / 2, 160 * px);
    alreadylabel.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    [alreadylabel setTitle:@"重新创建计划" forState:UIControlStateNormal];
    [alreadylabel setTitleColor:WYCMain forState:UIControlStateNormal];
    [alreadylabel addTarget:self action:@selector(alreadylabel) forControlEvents:UIControlEventTouchUpInside];
    alreadylabel.titleLabel.font = [UIFont systemFontOfSize:BigFont];
    [bottomView addSubview:alreadylabel];
    
    
    
    //执行该计划
    UIButton * payofflabel = [[UIButton alloc] init];
    payofflabel.frame = CGRectMake(bottomView.width / 2, alreadylabel.y, alreadylabel.width, alreadylabel.height);
    payofflabel.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    [payofflabel setTitle:@"执行该计划" forState:UIControlStateNormal];
    payofflabel.backgroundColor = WYCMain;
    [payofflabel addTarget:self action:@selector(payofflabel) forControlEvents:UIControlEventTouchUpInside];
    [payofflabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    payofflabel.titleLabel.font = [UIFont systemFontOfSize:BigFont];
    [bottomView addSubview:payofflabel];
    
}
-(void)payofflabel
{
    WYCLog(@"_planNo%@",_planNo);
    
    if (_planNo.length == 0) {
        
    }
    else
    {
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"token"] = [WYCAccountTool unarchivetoken];
        params[@"planNo"] = _planNo;
        [HttpTool POST:epaymentplanbank params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            
            WYCLog(@"-----------responseobject%@",responseObject);
            NSLog(@"计划创建返回信息%@",[responseObject objectForKey:@"msg"]);
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                
                
                
                [self showToast:@"计划创建成功"];
                
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[WYCSeeBillController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                        break;
                    }
                }
                WYCSeeBillController *SendDrugVC = [[WYCSeeBillController alloc]init];
                [self.navigationController popToViewController:SendDrugVC animated:YES];
                
            }
            else{
                [self showToast:@"计划创建失败"];
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            [self showToast:Failed];
        }];
    }
    
}
- (void)setupNav{
    
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.view.backgroundColor = WYCGlobalBg;
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCMain;
    [self.view addSubview:NavView];
    
    
    
    self.bottomLineLayer = [[CALayer alloc] init];
    self.bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    self.bottomLineLayer.backgroundColor = WYCRGBColor(191, 191, 196).CGColor;
    [NavView.layer addSublayer:self.bottomLineLayer];
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"yiwen"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(NavView.width - 96 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:backbutton];
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, fanhuibutton.y, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"预览消费计划";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)photoClick{
    
    self.ejectView = [[UIView alloc] init];
    self.ejectView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.ejectView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [[UIApplication sharedApplication].keyWindow  addSubview:self.ejectView];
    
    UIView * showView = [[UIView alloc] init];
    showView.frame = CGRectMake(80 * px, ScreenHeight / 2 - 320 * px, ScreenWidth - 160 * px, 640 * px);
    showView.layer.cornerRadius = 3;
    showView.layer.masksToBounds = YES;
    showView.backgroundColor = [UIColor whiteColor];
    [self.ejectView addSubview:showView];
    self.showView = showView;
    
    
    UILabel * redview = [[UILabel alloc] init];
    redview.frame = CGRectMake(0, 0, showView.width, 160 * px);
    redview.layer.cornerRadius = 3;
    redview.text = @"手续费说明";
    redview.textColor = [UIColor whiteColor];
    redview.textAlignment = NSTextAlignmentCenter;
    redview.layer.masksToBounds = YES;
    redview.font = [UIFont boldSystemFontOfSize:SuperBigFont];
    redview.backgroundColor = WYCRGBColor(200, 84, 53);
    [showView addSubview:redview];
    
    UILabel * shouxulabel = [[UILabel alloc] init];
    shouxulabel.frame = CGRectMake(40 * px, CGRectGetMaxY(redview.frame) + 40 * px, showView.width, 60 * px);
    shouxulabel.text = @"手续费由两部分组成";
    shouxulabel.font = [UIFont systemFontOfSize:MiddleFont];
    shouxulabel.textColor = [UIColor grayColor];
    [showView addSubview:shouxulabel];
    
    UILabel * huankuan = [[UILabel alloc] init];
    huankuan.frame = CGRectMake(40 * px, CGRectGetMaxY(shouxulabel.frame) + 40 * px, showView.width, 60 * px);
    huankuan.text = @"还款手续费=还款金额*0.8%";
    huankuan.font = [UIFont systemFontOfSize:MiddleFont];
    huankuan.textColor = [UIColor grayColor];
    [showView addSubview:huankuan];
    
    
    UILabel * huankuancishu = [[UILabel alloc] init];
    huankuancishu.frame = CGRectMake(40 * px, CGRectGetMaxY(huankuan.frame) + 40 * px, showView.width, 60 * px);
    huankuancishu.text = @"还款次数费=1.0元/次";
    huankuancishu.font = [UIFont systemFontOfSize:MiddleFont];
    huankuancishu.textColor = [UIColor grayColor];
    [showView addSubview:huankuancishu];
    
    
    UIView * lineview = [[UIView alloc] init];
    lineview.frame = CGRectMake(0, 480 * px, showView.width, 0.5);
    lineview.backgroundColor = [UIColor grayColor];
    [showView addSubview:lineview];
    
    UIButton * zhidao = [[UIButton alloc] init];
    zhidao.frame = CGRectMake(0, 520 * px, showView.width, 60 * px);
    [zhidao addTarget:self action:@selector(zhidaoClick) forControlEvents:UIControlEventTouchUpInside];
    zhidao.titleLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    zhidao.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [zhidao setTitleColor:WYCMain forState:UIControlStateNormal];
    [zhidao setTitle:@"我知道了" forState:UIControlStateNormal];
    
    [showView addSubview:zhidao];
}
- (void)zhidaoClick{
    
    self.ejectView.hidden = YES;
    self.showView.hidden = YES;
}
-(void)loadplan
{
    [HttpTool POST:epaymentplan params:_parmDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * result = responseObject[@"result"];
            _planNo =  [result objectForKey:@"planNo"];
            
            _Repaymentlabel1.text=  [NSString stringWithFormat:@"￥%.2f元",[result[@"consumePoundage"] doubleValue]+[result[@"repaymentPoundage"]doubleValue]];
            _Statement1.text = [NSString stringWithFormat:@"￥%@元",result[@"amount"]];
            
            NSArray * dataArray = result[@"planDetail"];
            for (NSDictionary * dic in dataArray) {
                
                WYCpreModel * taranLationModel = [WYCpreModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:taranLationModel];
                NSArray * detailsArray = dic[@"detail"];
                NSMutableArray * arr = [[NSMutableArray alloc] init];
                for (NSDictionary * detailsDic in detailsArray) {
                    
                    WYCAmoutMdel * DetailModel = [WYCAmoutMdel mj_objectWithKeyValues:detailsDic];
                    [arr addObject:DetailModel];
                }
                taranLationModel.dataArray = arr;
            }
            
            
        }else{
            [self showToast:msgStr];
            
        }
        [self.tableView reloadData];
        
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    WYCpreModel * head = self.dataArray[section];
    return  head.dataArray.count ;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 160 * px;
}
- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    WYCpreModel * head = self.dataArray[section];
    
    return head.date;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 90 * px;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    WYCPreCell * cell = [WYCPreCell cellWithTableView:tableView];
    
    
    
    WYCpreModel * model = self.dataArray[indexPath.section];
    WYCAmoutMdel * DetailModel = model.dataArray[indexPath.row];
    
    cell.timeLabel.text = DetailModel.consumeDate;
    cell.proceduresLabel.text = [NSString stringWithFormat:@"手续费:￥%.2f",[DetailModel.poundage doubleValue]];
    
    NSString  *tye = DetailModel.type;
    //消费
    if ([tye isEqualToString:@"1"]) {
        cell.typeImg.image = [UIImage imageNamed:@"消费icon"];
        cell.planAmountLabel.text =  [NSString stringWithFormat:@"￥%.2f",  [DetailModel.planAmount doubleValue ]+[DetailModel.poundage doubleValue]];
    }
    else
    {
        cell.typeImg.image = [UIImage imageNamed:@"还款icon"];
        cell.planAmountLabel.text =  [NSString stringWithFormat:@"￥%.2f",  [DetailModel.planAmount doubleValue ]-[DetailModel.poundage doubleValue]];
    }
    
    return cell;
}
@end

